home *** CD-ROM | disk | FTP | other *** search
- /*
- *┌──────────────────────────────────────────────────────────────────────
- *│ File.........: DATECLS4.CPP
- *│ Date.........: Sunday 3/7/1993
- *│ Author.......: Kenneth A. Argo (CIS 71241,3635)
- *│ Copyright....: None! Use freely.
- *│ Version......: 4.1 Compile w/MSC++ 7.0
- *│ Usage........: General purpose date conversion, arithmetic,
- *│ : comparison, and formatting class
- *│
- *│ See DATE.H for acknowledgements and compile/link notes.
- *└──────────────────────────────────────────────────────────────────────
- */
-
- #include "datecls4.h"
-
- int Date::DisplayFormat=MDY;
-
- unsigned char Date::DisplayOptions='\0';
-
- const char *dayname[] = {"Sunday","Monday","Tuesday","Wednesday",
- "Thursday","Friday","Saturday"} ;
-
- const char *mname[] = {"January","February","March","April","May",
- "June","July","August","September","October","November","December"};
-
- static int GauDays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-
- ////////////////////////////////////////////////////////////
- // Constructors
- ////////////////////////////////////////////////////////////
-
- Date::Date()
- {
- month = day = day_of_week = 0;
- year = 0;
- julian = 0;
- }
-
- Date::Date (const long j) : julian(j)
- {
- julian_to_mdy ();
- }
-
- Date::Date (const int m, const int d, const int y) : month((unsigned char)m), day((unsigned char)d), year(y)
- {
- mdy_to_julian ();
- }
-
- Date::Date (char *dat)
- {
- if (!_stricmp(dat, "TODAY"))
- {
- struct _dosdate_t temp_date;
- _dos_getdate(&temp_date);
- month = temp_date.month;
- day = temp_date.day;
- year = temp_date.year;
- }
- else
- {
- month = (unsigned char)atoi(strtok(dat,"/-"));
- day = (unsigned char)atoi(strtok(NULL,"/-"));
- year = atoi(strtok(NULL," "));
- }
-
- mdy_to_julian ();
- }
-
- Date::Date (const _dosdate_t &ds)
- {
- month = ds.month;
- day = ds.day;
- year = ds.year;
- mdy_to_julian ();
- }
-
- Date::Date (const Date &dt)
- {
- month = dt.month;
- day = dt.day;
- year = dt.year;
- mdy_to_julian ();
- }
-
- Date::~Date()
- {
- if (buf != (char *)&BadDate)
- delete buf;
- }
-
- //////////////////////////////////////////////////////////////
- // Conversion operations
- //////////////////////////////////////////////////////////////
-
- Date::operator char *( void )
- {
- buf = new char[13];
-
- if (day==0 || month==0 || year==0)
- buf = (char *)&BadDate;
- else
- sprintf(buf,"%1d/%1d/%-4d",month,day,year);
-
- return buf;
- }
-
- //////////////////////////////////////////////////////////////
- // Date Arithmetic
- //////////////////////////////////////////////////////////////
-
- Date &Date::operator + (const long i)
- {
- return Date(julian + i);
- }
-
- Date &Date::operator + (const int i)
- {
- return Date(julian + (long)i);
- }
-
- Date &Date::operator - (const long i)
- {
- return Date(julian - i);
- }
-
- Date &Date::operator - (const int i)
- {
- return Date(julian - (long)i);
- }
-
- long Date::operator - (const Date &dt)
- {
- return ( julian - dt.julian );
- }
-
- Date &Date::operator += (const long i)
- {
- julian += i;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator -= (const long i)
- {
- julian -= i;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator ++()
- {
- julian++;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator ++(int)
- {
- julian++;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator --()
- {
- julian--;
- julian_to_mdy();
- return *this;
- }
-
- Date &Date::operator --(int)
- {
- julian--;
- julian_to_mdy();
- return *this;
- }
-
- //////////////////////////////////////////////////////////////
- // Date comparison
- //////////////////////////////////////////////////////////////
-
- int operator < (const Date &dt1, const Date &dt2)
- {
- return ( dt1.julian < dt2.julian );
- }
-
- int operator <= (const Date &dt1, const Date &dt2)
- {
- return ( (dt1.julian == dt2.julian) || (dt1.julian < dt2.julian) );
- }
-
- int operator > (const Date &dt1, const Date &dt2)
- {
- return ( dt1.julian > dt2.julian );
- }
-
- int operator >= (const Date &dt1, const Date &dt2)
- {
- return ( (dt1.julian == dt2.julian) || (dt1.julian > dt2.julian) );
- }
-
- int operator == (const Date &dt1, const Date &dt2)
- {
- return ( dt1.julian == dt2.julian );
- }
-
- int operator != (const Date &dt1, const Date &dt2)
- {
- return ( dt1.julian != dt2.julian );
- }
-
- ////////////////////////////////////////////////////////////////
- // Ostream operations
- ////////////////////////////////////////////////////////////////
-
- ostream &operator << (ostream &os, const Date &dt)
- {
- return os << dt.formatDate();
- }
-
- ostream &operator << (ostream &os, const _dosdate_t &dt)
- {
- return os << (int)dt.month << "/" << (int)dt.day << "/" << dt.year;
- }
-
- //////////////////////////////////////////////////////////////
- // Conversion routines
- //////////////////////////////////////////////////////////////
-
- void Date::julian_to_wday (void)
- {
- day_of_week = (unsigned char) ((julian + 2) % 7 + 1);
- }
-
- void Date::julian_to_mdy ()
- {
- long a,b,c,d,e,z,alpha;
- z = julian+1;
-
- // dealing with Gregorian calendar reform
-
- if (z < 2299161L)
- a = z;
- else
- {
- alpha = (long) ((z-1867216.25) / 36524.25);
- a = z + 1 + alpha - alpha/4;
- }
-
- b = ( a > 1721423 ? a + 1524 : a + 1158 );
- c = (long) ((b - 122.1) / 365.25);
- d = (long) (365.25 * c);
- e = (long) ((b - d) / 30.6001);
-
- day = (unsigned char)(b - d - (long)(30.6001 * e));
- month = (unsigned char)((e < 13.5) ? e - 1 : e - 13);
- year = (int)((month > 2.5 ) ? (c - 4716) : c - 4715);
- julian_to_wday ();
- }
-
- void Date::mdy_to_julian (void)
- {
- int a,b=0;
- int work_month=month, work_day=day, work_year=year;
-
- // correct for negative year
-
- if (work_year < 0)
- work_year++;
-
- if (work_month <= 2)
- {
- work_year--;
- work_month +=12;
- }
-
- // deal with Gregorian calendar
-
- if (work_year*10000. + work_month*100. + work_day >= 15821015.)
- {
- a = (int)(work_year/100.);
- b = 2 - a + a/4;
- }
-
- julian = (long) (365.25*work_year) +
- (long) (30.6001 * (work_month+1)) + work_day + 1720994L + b;
- julian_to_wday ();
- }
-
- ////////////////////////////////////////////////////////////////
- // Format routine
- ////////////////////////////////////////////////////////////////
-
- char *Date::formatDate (const int type) const
- {
- static char buf[40];
-
- _strnset( buf, '\0', sizeof(buf) );
-
- switch ( type )
- {
- case DAY:
-
- if ( (day_of_week < 1) || (day_of_week > 7) )
- strcpy(buf,"invalid day");
- else
- strncpy( buf, dayname[day_of_week-1],
- (DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
-
- return buf;
- break;
-
- case MONTH:
-
- if ( (month < 1) || (month > 12) )
- strcpy(buf,"invalid month");
- else
- strncpy( buf, mname[month-1],
- (DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
-
- return buf;
- break;
-
- case FULL:
-
- if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
- (day_of_week > 7) )
- {
- strcpy(buf,"invalid date");
- return buf;
- }
-
- strncpy( buf, dayname[day_of_week-1],
- (DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
- strcat( buf, ", ");
- strncat( buf, mname[month-1],
- (DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
- strcat( buf, " ");
- sprintf( buf+strlen(buf), "%d, %d", day, abs(year) );
-
- if (year < 0)
- strcat(buf," B.C.E.");
-
- return buf;
- break;
-
- case EUROPEAN:
-
- if ( (month < 1) || (month > 12) || (day_of_week < 0) ||
- (day_of_week > 7) )
- {
- strcpy(buf,"invalid date");
- return buf;
- }
-
- sprintf(buf,"%d ", day);
- strncat(buf, mname[month-1],
- (DisplayOptions & DATE_ABBR) ? ABBR_LENGTH : 9);
- sprintf( buf+strlen(buf), " %d", abs(year) );
-
- if (year < 0)
- strcat(buf," B.C.E.");
-
- return buf;
- break;
-
- case MDY:
-
- default:
- if (day==0 || month==0 || year==0)
- strcpy(buf,"invalid date");
- else
- sprintf( buf+strlen(buf), "%1d/%1d/%02d", month, day,
- (DisplayOptions & NO_CENTURY) && (abs(year) > 1899)
- ? (abs(year) - (abs(year) / 100 * 100))
- : (abs(year)) );
-
- return buf;
- break;
- }
- }
-
- void Date::setFormat( const int format )
- {
- DisplayFormat = format;
- }
-
- int Date::setOption( const int option, const int action )
- {
- switch ( option )
- {
- case NO_CENTURY:
-
- if ( action )
- DisplayOptions |= NO_CENTURY;
- else
- {
- DisplayOptions &= (~NO_CENTURY);
- }
-
- return 1;
- break;
-
- case DATE_ABBR:
-
- if ( action )
- DisplayOptions |= DATE_ABBR;
- else
- {
- DisplayOptions &= (~DATE_ABBR);
- }
-
- return 1;
- break;
-
- default:
-
- return 0;
- break;
-
- }
- }
-
- ///////////////////////////////////////////////////////////////
- // Miscellaneous Routines
- ///////////////////////////////////////////////////////////////
-
- long Date::julDate( void ) const
- {
- return julian;
- }
-
- int Date::DOY( void ) const
- {
- Date temp( 1, 1, year );
-
- return (int) (julian - temp.julian + 1);
- }
-
-
- int Date::isLeapYear( void ) const
- {
- return ( (year >= 1582) ?
- (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ):
- (year % 4 == 0) );
- }
-
- _dosdate_t Date::eom( void ) const
- {
- static _dosdate_t eom_temp;
- Date tempdate( (month % 12) + 1, 1, year);
-
- if (month == 12)
- tempdate.year++;
-
- tempdate--;
-
- eom_temp.year = tempdate.year;
- eom_temp.month = tempdate.month;
- eom_temp.day = tempdate.day;
-
- return eom_temp;
- }
-
- _dosdate_t Date::getDate( void ) const
- {
- static _dosdate_t getDate_temp;
-
- getDate_temp.year = year;
- getDate_temp.month = month;
- getDate_temp.day = day;
-
- return getDate_temp;
- }
-
-
- //─────────────────────────────────────────────────
- // Version 4.0 Extension to Public Interface - CDP
- //─────────────────────────────────────────────────
-
- PUBLIC Date &
- Date::Set()
- {
- struct _dosdate_t sDate;
- _dos_getdate(&sDate);
-
- month = sDate.month;
- day = sDate.day;
- year = sDate.year;
-
- mdy_to_julian();
- return *this;
- }
-
- PUBLIC Date &
- Date::Set(int nMonth, int nDay, int nYear)
- {
- month = (unsigned char)nMonth;
- year = nYear < 0 ? 9999 : nYear;
- year = nYear > 9999 ? 0 : nYear;
- day = (unsigned char)(nDay < DaysInMonth() ? nDay : DaysInMonth());
-
- mdy_to_julian();
- return *this;
- }
-
- PUBLIC Date &
- Date::Set(long j)
- {
- julian = j;
-
- julian_to_mdy();
- return *this;
- }
-
-
- PUBLIC int Date::DaysInMonth()
- {
- return GauDays[month-1] + (month==2 && isLeapYear());
- }
-
- PUBLIC int Date::FirstDOM() const
- {
- return Date(month, 1, year).NDOW();
- }
-
- PUBLIC int Date::Day() const
- {
- return day;
- }
-
- PUBLIC int Date::NDOW() const
- {
- return day_of_week;
- }
-
- PUBLIC int Date::NYear4() const
- {
- return year;
- }
-
- PUBLIC int Date::NMonth() const
- {
- return month;
- }
-
- PUBLIC Date & Date::AddWeeks(int nCount)
- {
- Set(julian + (long)nCount*7);
- return *this;
- }
-
-
- PUBLIC Date & Date::AddMonths(int nCount)
- {
- month += (unsigned char)nCount;
-
- if (month < 1)
- {
- month = 12;
- year--;
- }
-
- if (month > 12)
- {
- month = 1;
- year++;
- }
-
- mdy_to_julian();
- return *this;
- }
-
- PUBLIC Date & Date::AddYears(int nCount)
- {
- year += nCount;
- mdy_to_julian();
- return *this;
- }
-
- PUBLIC int Date::WOM()
- {
- // Abs day includes the days from previous month that fills up
- // the begin. of the week.
- int nAbsDay = day + FirstDOM()-1;
- return (nAbsDay-NDOW())/7 + 1;
- }
-
- PUBLIC int Date::WOY()
- {
- // ErrFail();
- Date doTemp(1, 1, year);
- return (int)(((julian - doTemp.julian+1)/7) + 1);
- }
-
- PUBLIC Date Date::BOM()
- {
- return(Date(month, 1, year));
- }
-
- PUBLIC Date Date::EOM()
- {
- return(Date(month+1, 1, year)-1);
- }
-
- PUBLIC Date Date::BOY()
- {
- return(Date(1, 1, year));
- }
-
- PUBLIC Date Date::EOY()
- {
- return(Date(1, 1, year+1)-1);
- }
-
- PUBLIC char * Date::CMonth()
- {
- return(formatDate(MONTH));
- }
-
- PUBLIC char * Date::CDOW()
- {
- return(formatDate(DAY));
- }
-